package com.jmlee.service.impl;

import com.jmlee.mapper.MenuMapper;
import com.jmlee.mapper.RoleMapper;
import com.jmlee.pojo.Menu;
import com.jmlee.pojo.Role;
import com.jmlee.service.MenuService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public Map<String,Object> getMenuInfo(Long rId) {
            Map<String,Object> userMenuInfo = new LinkedHashMap<>();
            List<Object> menuList = new LinkedList<>();
            List<Menu> parentMenu = menuMapper.selectMenuByRidAndParentId(rId,0L,1);

            Iterator<Menu> parentMenuIt = parentMenu.iterator();
            while (parentMenuIt.hasNext()){
               Menu menu = parentMenuIt.next();
               if (menu.getStatus() == 0){
                    parentMenuIt.remove();
                }
                if (menu.getStatus() != 0) {
                    List<Menu> childrenMenu = menuMapper.selectMenuByRidAndParentId(rId, menu.getId(), 1);
                    childrenMenu.removeIf(childMenu -> childMenu.getStatus() == 0);
                    menu.setChildren(childrenMenu);
                    menuList.add(menu);
                }
            }
            userMenuInfo.put("menu",menuList);
            return userMenuInfo;
    }



    @Override
    public List<Object> getMenuInfoByRid(Long rId) {
        List<Object> menuTreeList = new LinkedList<>();
        List<String> enableChildMenuId = getEnableListId(rId,1L);
        List<String> enableParentMenuId = getEnableListId(rId,0L);
        List<Menu> parentMenu = menuMapper.selectMenuByRidAndParentId(rId,0L,1);
        //父菜单
        for (Menu menu:parentMenu){
            Map<String,Object> menuTreeMap = new LinkedHashMap<>();
            List<Menu> childrenMenu = menuMapper.selectMenuByRidAndParentId(rId,menu.getId(),1);
            List<Object> childrenMenuTreeList = new LinkedList<>();
            menuTreeMap.put("title",menu.getTitle());
            menuTreeMap.put("id",menu.getId());
            if (menu.getId() == 3L){
                menuTreeMap.put("disabled",true);
            }
            //启用的父菜单ID
            for (String id:enableParentMenuId ) {
                if (Long.parseLong(id) == menu.getId()){
                    menuTreeMap.put("checked",true);
                }
            }
            menuTreeMap.put("spread",true);
            //子菜单
            for (Menu cMenu:childrenMenu) {
                Map<String,Object> cMenuTreeMap = new LinkedHashMap<>();
                cMenuTreeMap.put("title",cMenu.getTitle());
                cMenuTreeMap.put("id",cMenu.getId());
                //启用的子菜单ID
                for (String id:enableChildMenuId) {
                    if (Long.parseLong(id) == cMenu.getId()){
                        cMenuTreeMap.put("checked",true);
                    }
                    if (cMenu.getParentId() == 3L){
                        cMenuTreeMap.put("disabled",true);
                    }
                }
                childrenMenuTreeList.add(cMenuTreeMap);
            }
            menuTreeMap.put("children",childrenMenuTreeList);
            menuTreeList.add(menuTreeMap);
        }
        return menuTreeList;
    }

    @Override
    public List<Menu> getAllParentMenuList() {
        return menuMapper.selectParentMenu();
    }

    @Override
    public List<Menu> getAllChildMenu() {
        return menuMapper.selectChildMenu();
    }

    @Override
    public List<String> getEnableListId(Long rId,Long parentId) {
        List<String> mIdList = new LinkedList<>();
        if (parentId == 0){
            mIdList = menuMapper.selectParentMenuIdByStatusAndRid(rId,1);
        }else {
            mIdList = menuMapper.selectChildMenuIdByStatusAndRid(rId,1);
        }
        return mIdList;
    }

    @Override
    public List<String> getUnableListId(Long rId,Long parentId) {
        List<String> mIdList = new LinkedList<>();
        if (parentId == 0){
            mIdList = menuMapper.selectParentMenuIdByStatusAndRid(rId,0);
        }else {
            mIdList = menuMapper.selectChildMenuIdByStatusAndRid(rId,0);
        }
        return mIdList;
    }



    @Override
    public Integer addMenu(Menu menu) {
        Integer status = 0;
        status =  menuMapper.insertNewMenu(menu);
        List<Role> roleList = roleMapper.selectRoleList();

        for (Role role:roleList) {
           status = menuMapper.insertMenuConnect(role.getId(),menu.getId());
        }

        return status;
    }

    @Override
    public Integer editMenu(Menu menu) {
        return menuMapper.updateMenuById(menu);
    }

    @Override
    public List<Long> allMenuId() {
        return menuMapper.selectAllMenuId();
    }

    @Override
    public List<Menu> allMenuList(RowBounds rowBounds, Menu menu) {
        return menuMapper.selectAllMenu(rowBounds, menu);
    }

    @Override
    public Long menuCount(Menu menu) {
        return menuMapper.selectCountMenu(menu);
    }

//    @Override
//    public Integer deleteMenu(Long id) {
//        return null;
//    }
}
